﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Repository.BasicSet;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Domain.ViewModels.BasicSet.MeterType;

namespace WaterRevenueMS.Core.Service.BasicSet.Impl
{
    [Export(typeof(IMeterTypeService))]
    public class MeterTypeService : CoreServiceBase, IMeterTypeService
    {
        [Import]
        public IMeterTypeRepository MeterTypeRepository { get; set; }

        public IQueryable<MeterType> MeterTypes
        {
            get { return MeterTypeRepository.Entities; }
        }

        public OperationResult Insert(MeterTypeModel model)
        {
            var entity = new MeterType
            {
                Yi_BLX = model.Yi_BLX,
                Lei_XDM = model.Lei_XDM,
                DefaultValue = model.DefaultValue == 0 ? (byte)0 : model.DefaultValue,
                CreateDT = DateTime.Now,
                ReviseDT = DateTime.Now,
                OrderNo = model.OrderNo,
                Tong_XXY = model.Tong_XXY
            };
            MeterTypeRepository.Insert(entity);
            return new OperationResult(OperationResultType.Success, "添加成功");
        }

        public OperationResult Update(MeterTypeModel model)
        {
            var entity = MeterTypeRepository.Entities.FirstOrDefault(t => t.Id == model.Id);
            if (entity != null)
            {
                entity.Yi_BLX = model.Yi_BLX;
                entity.Lei_XDM = model.Lei_XDM;
                entity.DefaultValue = model.DefaultValue;
                entity.ReviseDT = DateTime.Now;
                entity.OrderNo = model.OrderNo;
                entity.Tong_XXY = model.Tong_XXY;
            }
            MeterTypeRepository.Update(entity);
            return new OperationResult(OperationResultType.Success, "更新成功");
        }

        public OperationResult Delete(int Id)
        {
            var entity = MeterTypeRepository.Entities.FirstOrDefault(t => t.Id == Id);
            if (entity != null)
            {
                MeterTypeRepository.Delete(entity);
            }
            return new OperationResult(OperationResultType.Success, "删除成功");
        }
    }
}
